package com.coder4j.log.controller;

import com.coder4j.log.entity.ErrorLogEntity;
import com.coder4j.log.service.IErrorLogService;
import com.coder4j.log.vo.ErrorLogQuery;
import com.coder4j.log.vo.ErrorLogVO;
import com.coder4j.log.vo.ErrorLogEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统错误日志
 * @author keller
 * @date 2024-05-07 18:18:57
 */
@Slf4j
@RestController
@RequestMapping("/errorLog")
public class ErrorLogController {
    @Resource
    private IErrorLogService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询系统错误日志
     * @param id ErrorLog.id
     * @return 系统错误日志对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询系统错误日志",createName = "keller",createTime = "2024-05-07 18:18:57")
    public R<ErrorLogVO> getById( Long id){

        log.info("====== ErrorLogControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }


    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询系统错误日志列表",createName = "keller",createTime = "2024-05-07 18:18:57")
    public R<BasePage<ErrorLogVO>> getList(@RequestBody ErrorLogQuery query){
        if(query == null){
            query = new ErrorLogQuery();
        }
        log.info("====== ErrorLogControllerGetList ====== {}",query);
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出系统错误日志列表",createName = "keller",createTime = "2024-05-07 18:18:57")
    public void export(@RequestBody ErrorLogQuery query){
        if(query == null){
            query = new ErrorLogQuery();
        }
        log.info("====== ErrorLogControllerGetList ====== {}",query);
        List<ErrorLogEntity> list = service.baseQueryList(query);
                List<ErrorLogEO> eoList = new ArrayList<>();
                for (ErrorLogEntity entity : list) {
                    ErrorLogEO eo = new ErrorLogEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"ErrorLogList",eoList);
    }


    /**
     * 删除系统错误日志
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id ErrorLog.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除系统错误日志",createName = "keller",createTime = "2024-05-07 18:18:57")
    public R deleteById( Long id){
        log.info("====== ErrorLogControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);
        return R.success();
    }
}
